package cn.flyboy.service.impl;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cn.flyboy.entity.the_exam;
import cn.flyboy.mapper.ExamMapper;
import cn.flyboy.entity.Answer;
import cn.flyboy.entity.Exam;
import cn.flyboy.mapper.StudentMapper;
import cn.flyboy.service.ExamService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class ExamServiceImpl implements ExamService {

    @Resource
    private ExamMapper examMapper;
    @Resource
    private StudentMapper studentMapper;

    @Override
    public int insExam(Exam exam) {
        return examMapper.insExam(exam);
    }

    @Override
    public List<Exam> getAllTeaExam(String teaID) {
        return examMapper.getAllTeaExam(teaID);
    }

    @Override
    public List<Exam> selAllExam() {
        return examMapper.selAllExam();
    }

    @Override
    public List<the_exam> getAllExamFromStu(String stuId) {
        return examMapper.selAllExamFromStu(stuId);
    }

    @Override
    public int updExam(String e_oldname, Exam exam) {
        return examMapper.updExam(e_oldname, exam);
    }

    @Override
    public int updStopExam(String name) {
        return examMapper.updStopExam(name);
    }

    @Override
    public int updClearExam(String name) {
        return examMapper.updClearExam(name);
    }

    @Override
    public List<Exam> selStartExam() {
        return examMapper.selStartExam();
    }

    @Override
    public boolean saveAnswer(MultipartFile multipartFile, HttpServletRequest req, HttpSession session, String stu_id, String stu_exam) {
        String StartExamTeaName = examMapper.selectStartExamTeaName(stu_exam);

        String originalFileName = multipartFile.getOriginalFilename();
        String path = req.getServletContext().getRealPath("/files/" + stu_exam);

        File targetFilePath = new File(path, originalFileName);
        System.out.println(targetFilePath + "+++++++++++++++++++++++");

        if (!targetFilePath.exists()) {
            targetFilePath.getParentFile().mkdirs();
            try {
                if (targetFilePath.createNewFile()) {
                    multipartFile.transferTo(targetFilePath);
                    Answer answer = new Answer();
                    answer.setStu_id(stu_id);
                    answer.setExam_name(stu_exam);
                    answer.setTea_name(StartExamTeaName);
                    answer.setFile_name(originalFileName);
                    answer.setFile_size("" + multipartFile.getSize());
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                    answer.setTimes(df.format(new Date()));
                    int index1 = examMapper.saveAnswer(answer);
                    int index2 = studentMapper.editStudentOfSubmit(stu_id, stu_exam);
                    if (index1 >= 1 && index2 >= 1) {
                        return true;
                    } else {
                        return false;
                    }
                }
            } catch (IOException e) {
                return false;
            }
        }
        if (targetFilePath.exists()) {
            try {
                multipartFile.transferTo(targetFilePath);
                Answer answer = new Answer();
                answer.setStu_id(stu_id);
                answer.setExam_name(stu_exam);
                answer.setTea_name(StartExamTeaName);
                answer.setFile_name(originalFileName);
                answer.setFile_size("" + multipartFile.getSize());
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                answer.setTimes(df.format(new Date()));
                int index1 = examMapper.saveAnswer(answer);
                int index2 = studentMapper.editStudentOfSubmit(stu_id, stu_exam);
                if (index1 >= 1 && index2 >= 1) {
                    return true;
                } else {
                    return false;
                }
            } catch (IOException e) {
                return false;
            }
        }
        return false;
    }

    @Override
    public boolean savePager(MultipartFile multipartFile, HttpServletRequest req, HttpSession session) throws IOException {
        Exam exam = (Exam) session.getAttribute("exam");

        // 获取文件名
        String originalFileName = multipartFile.getOriginalFilename();

        // 上传路径保存设置
        String path = req.getServletContext().getRealPath("/upload/" + exam.getE_name());

        // (String parent,String Child)
        File targetFilePath = new File(path, originalFileName);

        if (!targetFilePath.exists()) {
            targetFilePath.getParentFile().mkdirs(); //先创父文件夹

            if (targetFilePath.createNewFile()) {  // 创建子文件
                System.out.println(targetFilePath);
                multipartFile.transferTo(targetFilePath);

                examMapper.savePager(exam.getE_name(), originalFileName);
                exam.setE_examination(originalFileName);
                session.setAttribute("exam", exam);
            }
            return true;
        }
        return false;
    }

    @Override
    public int examStart(String e_name) {
        return examMapper.updIsStart(e_name);
    }

    @Override
    public String selFileByName(String e_name, String e_teacher) {
        return examMapper.selFileByName(e_name, e_teacher);
    }

    @Override
    public String selectStartExamTeaName(String e_name) {
        return examMapper.selectStartExamTeaName(e_name);
    }
}
